var config = require("config");
var dialogManager = require("dialogManager");
cc.Class({
    extends: cc.Component,

    properties: {
        record_all : {
            default : null,
            type : cc.Label
        },
        record_cur : {
            default : null,
            type : cc.Label
        },
        gold_all : {
            default : null,
            type : cc.Label
        },
        gold_get : {
            default : null,
            type : cc.Label
        },
        bg : {
            default : null,
            type : cc.Node
        },
    },
    onLoad () { 
        var height = cc.view.getVisibleSize().height;
        this.node.height = height;
        this.bg.height = height/2*1.3;
        config.loadImage(this.bg,"pet_bg","resources/image/");
    },
    show(args){
        var self = this;
        config.gotoUpLv = false;
        config.LaserNum = 0;
        config.isBossShow = false;
        config.CurHeroWuDi = false;
        config.isToHero = false;
        config.HeroState = 0;
        config.doubleSpeed = false;
        config.ScoreTime=60;

        this.record_cur.string = args.record_cur;
        this.gold_get.string = args.gold_get;
        config.AllRecordMi = config.AllRecordMi + parseInt(args.record_cur);//总里程
        config.PlayerGold = config.PlayerGold + parseInt(args.gold_get); //玩家金币
        this.record_all.string = config.AllRecordMi;
        this.gold_all.string = config.PlayerGold;
        cc.vv.gameDataMgr.saveGold(config.PlayerGold);
        cc.vv.gameDataMgr.saveAllRecordMi(config.AllRecordMi);

        //更新记录
        if(config.MaxRecordMi < parseInt(args.record_cur)){
            config.MaxRecordMi = parseInt(args.record_cur);
            cc.vv.gameDataMgr.saveMaxRecordMi(config.MaxRecordMi);
            this.setUserCloud(config.MaxRecordMi);
        }

        var gameNum = cc.vv.gameDataMgr.getGameNumber()+1;
        cc.vv.gameDataMgr.saveGameNumber(gameNum);

        if (cc.sys.OS_ANDROID == cc.sys.os && config.Publish == "android"){
            return;
        }

        if(gameNum == 2 && args.gold_get>=1000){
            var callFunc = function(){
                config.PlayerGold = config.PlayerGold + parseInt(args.gold_get)*19; //玩家金币
                self.gold_all.string = config.PlayerGold;
                cc.vv.gameDataMgr.saveGold(config.PlayerGold);
            }
            var args2 = {}
            args2.callFun = callFunc;
            args2.rate_txt = 20;
            dialogManager.showShiPinDialog(args2);
            return;
        }
        
        if(args.KanShiPin && args.gold_get>=500){
            var num = Math.floor(Math.random()*30);
            if(num<10){
                var randomNum = Math.floor(Math.random()*40)+40;
                var callFunc = function(){
                    config.PlayerGold = config.PlayerGold + parseInt(args.gold_get)*randomNum; //玩家金币
                    self.gold_all.string = config.PlayerGold;
                    cc.vv.gameDataMgr.saveGold(config.PlayerGold);
                }
                var args2 = {}
                args2.callFun = callFunc;
                args2.rate_txt = randomNum+1;
                dialogManager.showShiPinDialog(args2);
            }
        }
    },
    closeClick(){
        cc.vv.audioMgr.playSFX("ui_button");
        // this.node.destroy();
        cc.director.preloadScene("hallScene", function () {
            cc.director.loadScene("hallScene");
        });
    },
    restartGameClick(){
        cc.director.preloadScene("gameScene", function () {
            cc.director.loadScene("gameScene");
        });
    },
    gotoLvUpClick(){
        config.gotoUpLv = true;
        cc.director.preloadScene("hallScene", function () {
            cc.director.loadScene("hallScene");
        });
    },
    //向微信发数据
    setUserCloud(recordMi){
        if(typeof(wx)=="undefined"){return;}

        console.log(">>>>向微信发数据");

        var timestamp = Date.parse(new Date());
        var wxValue = {
            "wxgame": {
                "score": recordMi,
                "update_time": timestamp,
            },
        }
        var valueStr = JSON.stringify(wxValue)
        console.log(valueStr);
        var kvDataList = new Array();
        kvDataList.push({
            key: "score",
            value: valueStr
        });
        wx.setUserCloudStorage({
            KVDataList: kvDataList
        })
        
    }
});
